<?php
App::uses('AppController', 'Controller');
/**
 * Drives Controller
 *
 * @property Drive $Drive
 */
class DrivesController extends AppController {


public $components = array(
        'RequestHandler'
    );
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Drive->recursive = 0;
		$this->set('drives', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Drive->id = $id;
		if (!$this->Drive->exists()) {
			throw new NotFoundException(__('Invalid drive'));
		}
		$this->set('drive', $this->Drive->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Drive->create();
			if ($this->Drive->save($this->request->data)) {
				$this->Session->setFlash(__('The drive has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The drive could not be saved. Please, try again.'));
			}
		}
		$users = $this->Drive->User->find('list');
		$contracts = $this->Drive->Contract->find('list');
		$this->set(compact('users', 'contracts'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Drive->id = $id;
		if (!$this->Drive->exists()) {
			throw new NotFoundException(__('Invalid drive'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Drive->save($this->request->data)) {
				$this->Session->setFlash(__('The drive has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The drive could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Drive->read(null, $id);
		}
		$users = $this->Drive->User->find('list');
		$contracts = $this->Drive->Contract->find('list');
		$this->set(compact('users', 'contracts'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Drive->id = $id;
		if (!$this->Drive->exists()) {
			throw new NotFoundException(__('Invalid drive'));
		}
		if ($this->Drive->delete()) {
			$this->Session->setFlash(__('Drive deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Drive was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

	public function getForm(){
		if ($this->RequestHandler->isAjax())
		{
			$this->loadModel('User');
			if($this->request->data['Drive']['nature'] == "Outros"){
				$this->render('/Elements/formOthers');	
			}elseif($this->request->data['Drive']['nature'] == "Pagamento de salário"){
				$user = $this->User->find("list", array('fields' => array('User.id', 'User.name')));
				$this->set('users', $user);
				$this->render('/Elements/formSalaPayment');	
			}
			
		}
		
	}
}
